package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserEdu;
import com.lantosec.hmrp.modules.archives.mapper.UserEduMapper;
import com.lantosec.hmrp.modules.archives.service.IUserEduService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserEduVO;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 教育信息表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-09
 */
@Service
@Primary
@Log4j2
public class UserEduServiceImpl extends ServiceImpl<UserEduMapper, UserEdu> implements IUserEduService {
    @Autowired
    private ISysDictionariesService iSysDictionariesService;

    @Autowired
    private ISysUploadsService iSysUploadsService;

    @Override
    public void saveUserEdu(UserEduVO vo) {
        UserEdu userEdu = vo.convertToEntity();
        userEdu.setUserEduId(UUIDUtils.get());
        //获取当前用户id和名称
        userEdu.setCreateId(TokenUtil.getUserId());
        userEdu.setCreateUser(TokenUtil.getName());
        userEdu.setCreateTime(DateUtils.nowDate());
        userEdu.setVerifyStatus(1);
        this.save(userEdu);
    }

    @Override
    @Transactional
    public void updateUserEdu(UserEduVO vo) {
        String userEduId = vo.getUserEduId();
        //新增一条数据
        //saveUserEdu(vo);
        //修改这条数据的审核状态为2
        if (StringUtils.isNotEmpty(userEduId)) {
            UserEdu userEdu = vo.convertToEntity();
            userEdu.setVerifyStatus(1);
            this.updateById(userEdu);
        }else {
            saveUserEdu(vo);
        }
    }

    @Override
    public List<UserEduVO> getUserEdu(String userId) {
        List<UserEduVO> userEduVOList = new ArrayList<>();
        List<UserEdu> userEduList = this.list(new QueryWrapper<UserEdu>().eq(UserEdu.ColName.user_id.toString(), userId).orderByDesc(UserEdu.ColName.create_time.toString()));
        userEduList.forEach(param->{
            UserEduVO userEduVO = getUserEduVo(param);
            userEduVOList.add(userEduVO);
        });
        return userEduVOList;
    }

    /**
     * 组装用户教育信息
     *
     * @param userEdu
     * @return
     */
    private UserEduVO getUserEduVo(UserEdu userEdu) {
        UserEduVO userEduVO = userEdu.convertToVO();
        try {
            //查询字典表对应的学历
            String education = userEdu.getEducation();
            if(StringUtils.isNotEmpty(education)){
                String educationName = iSysDictionariesService.getById(education).getDictionariesShowName();
                userEduVO.setEducationName(educationName);
            }
            //查询字典表对应的学位
            String degree = userEdu.getDegree();
            if(StringUtils.isNotEmpty(degree)){
                String degreeName = iSysDictionariesService.getById(degree).getDictionariesShowName();
                userEduVO.setDegreeName(degreeName);
            }
            //查询学位证书影印件路径
            String diplomaPhotocopy = userEdu.getDiplomaPhotocopy();
            if(StringUtils.isNotEmpty(diplomaPhotocopy)){
                String[] diplomaPhotocopys = diplomaPhotocopy.split(",");
                List list = new ArrayList();
                for(String s : diplomaPhotocopys){
                    SysUploads sysUploads = iSysUploadsService.getById(s);
                    String savePath = sysUploads.getSavePath();
                    String fileName = sysUploads.getFileName();
                    list.add(InitDataListener.uploadsUrl + savePath + fileName);
                }
                userEduVO.setSavePath(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userEduVO;
    }

    @Override
    public List<UserEduVO> getUserEduVerify(String userId) {
        List<UserEduVO> userEduVOList = new ArrayList<>();
        List<UserEdu> userEduList = this.list(new QueryWrapper<UserEdu>().eq(UserEdu.ColName.create_id.toString(), userId).in(UserEdu.ColName.verify_status.toString(), 0, 2));
        userEduList.forEach(param -> {
            UserEduVO userEduVO = getUserEduVo(param);
            userEduVOList.add(userEduVO);
        });
        return userEduVOList;
    }

    @Override
    public void verifyUserEdu(UserEduVO vo) {
        UserEdu userEdu = vo.convertToEntity();
        Integer verifyStatus = userEdu.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userEdu.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userEdu.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userEdu.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userEdu.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userEdu.setVerifyId(TokenUtil.getUserId());
        userEdu.setVerifyUser(TokenUtil.getName());
        userEdu.setVerifyTime(DateUtils.nowDate());
        this.updateById(userEdu);
    }

    @Override
    public List<UserEduVO> getUserEduHistory(String userId) {
        List<UserEduVO> userEduVOList = new ArrayList<>();
        List<UserEdu> userEduList = this.list(new QueryWrapper<UserEdu>().eq(UserEdu.ColName.create_id.toString(), userId).eq(UserEdu.ColName.verify_status.toString(), 4)
                .orderByDesc(UserEdu.ColName.create_time.toString()));
        userEduList.forEach(param -> {
            UserEduVO userEduVO = getUserEduVo(param);
            userEduVOList.add(userEduVO);
        });
        return userEduVOList;
    }
}
